//
//(C) Copyright 2011 HongKong Airline.,Ltd. 
//
/**
 * menuinfoServiceImpl
 * 项目名称:代购系统
 *
 * @author Mengxiangliang
 *
 * 功能描述:
 * 
 * 历史版本:2013-01-04 v1.0.0 Mengxiangliang  创建:
 * 
 */
 
package com.gamutsoft.amb.menuinfo.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.commons.collections.map.LinkedMap;

import com.gamutsoft.amb.common.bean.QueryExample;
import com.gamutsoft.amb.common.exception.BusinessException;
import com.gamutsoft.amb.common.service.impl.GenericServiceImpl;
import com.gamutsoft.amb.menuinfo.dao.IMenuinfoDao;
import com.gamutsoft.amb.menuinfo.model.Menuinfo;
import com.gamutsoft.amb.menuinfo.service.IMenuinfoService;

public class MenuinfoServiceImpl extends GenericServiceImpl<Menuinfo, Long> implements IMenuinfoService {
	public IMenuinfoDao menuinfoDao;

    public MenuinfoServiceImpl(IMenuinfoDao menuinfoDao) {
		super(menuinfoDao);
		this.menuinfoDao = menuinfoDao;
	}
	
	/**
	 * 根据条件查询记录
	 * @author Mengxiangliang
	 * @param  QueryExample
	 * @return MenuinfoList
	 * @throws BusinessException
	 */
	public List<Map> searchMenuinfoList(QueryExample qe) throws BusinessException{
	  
	   return menuinfoDao.searchMenuinfoList(qe);
	   
	}
	
	/**
	 * 根据ID查询记录
	 * @author Mengxiangliang
	 * @param Long id
	 * @return domain
	 * @throws BusinessException
	 */
	public Menuinfo searchMenuinfoById(Long id) throws BusinessException{
	  
	   return menuinfoDao.searchMenuinfoById(id);
	   
	}

	/**
	 *  删除记录
	 * @author Mengxiangliang	 
	 * @param id
	 * @throws BusinessException
	 */
	public void deleteMenuinfoById(Long id) throws BusinessException{
	 
	  menuinfoDao.deleteMenuinfoById(id);
	  
	}
	
	/**
	 * 修改记录
	 * @author Mengxiangliang
	 * @param domain
	 * @throws BusinessException
	 */
	public void updateMenuinfo(Menuinfo domain) throws BusinessException{
	
	   menuinfoDao.save(domain);
	   
	}

	/**
	 * 新增记录
	 * @author Mengxiangliang
	 * @param domain
	 * @return
	 * @throws BusinessException
	 */
	public void createMenuinfo(Menuinfo domain) throws BusinessException{
	
	   menuinfoDao.save(domain);
	}

	@Override
	public List<Map> searchMenuinfoByWhere(QueryExample qe, Menuinfo domain)
			throws BusinessException {
		// TODO Auto-generated method stub
		return menuinfoDao.searchMenuinfoByWhere(qe, domain);
	}

	@Override
	public void deleteByParentCode(String parentCode) throws BusinessException {
		// TODO Auto-generated method stub
		menuinfoDao.deleteByParentCode(parentCode);
	}

	@Override
	//只适用于两级关系
	public Map<String, List<Map>> searchMenuInfoByType(String type)
			throws BusinessException {
		Map<String, List<Map>> resultMap=new LinkedMap();
		if(type!=null && !"".equals(type)){
		  QueryExample qe=new QueryExample ();
		  String mysql=" T.CODE like '"+type+"%' ";
		  qe.setMySql(mysql);
		  List<Map>list=searchMenuinfoByWhere(qe,null);
		  if(list!=null && list.size()!=0){
			  //分类分装
			  for(Map map:list){
				  String code=map.get("code").toString();
				  String parentCode=map.get("parentcode").toString();
				  if(!type.equals(code)){//过滤掉根节点
					  if(type.equals(parentCode)){//第一层节点
						  if(!resultMap.containsKey(code)){
							  List<Map> children=new ArrayList();
							  resultMap.put(code, children);
						  }
					  }
				  }
			  }
			  //装入子
			  for(Map map:list){
				  String code=map.get("code").toString();
				  String parentCode=map.get("parentcode").toString();
				  if(!type.equals(code)){//过滤掉根节点
					  if(!type.equals(parentCode) && resultMap.containsKey(parentCode)){
						  resultMap.get(parentCode).add(map);
					  }
				  }
			  }
		  }
		}
		return resultMap;
	}

	@Override
	public String generateMenuStr(String rootCode) throws BusinessException {
		StringBuffer result=new StringBuffer("[");
		if(rootCode!=null && !"".equals(rootCode)){
			Map<String, List<Map>> menuMap=searchMenuInfoByType(rootCode);
			if(menuMap!=null){
			     for (String key : menuMap.keySet()) {
			    	 Menuinfo domain=menuinfoDao.searchMenuByCode(key);
			    	// JSONArray jsonArrayBm = JSONArray.fromObject(null);
			    	 result.append("{");
			    	 result.append("'showName':'"+domain.getName()+"',");
			    	 result.append("'code':'"+domain.getCode()+"',");
			    	 result.append("'url':'"+domain.getUrl()+"',");
			    	 result.append("'subNodes':[");
			    	 List<Map>list=menuMap.get(key);
			    	 if(list!=null && list.size()!=0){
			    		 for(int i=0;i<list.size();i++){
			    			 result.append("{");
			    			 result.append("'showName':'"+list.get(i).get("name").toString()+"',");
					    	 result.append("'code':'"+list.get(i).get("code").toString()+"',");
					    	 result.append("'url':'"+list.get(i).get("url").toString()+"'");
					    	 if(i==list.size()-1){
					    		 result.append("}");
					    	 }else{
					    		 result.append("},");
					    	 }
			    		 }
			    	 }
			    	 result.append("]");
			    	 result.append("},");
				 }
			}
		}
		result.delete(result.length()-1, result.length());//删除最后一个逗号
		result.append("]");
		return result.toString();
	}

	@Override
	public Menuinfo searchMenuByCode(String code) throws BusinessException {
		// TODO Auto-generated method stub
		return menuinfoDao.searchMenuByCode(code);
	}
	

}